package org.cocopapaya.anttsp.domain;

import java.util.Collection;

import edu.uci.ics.jung.graph.Graph;

public class QuantityField {

	private static final double DISSIPATION_FACTOR = 0.2;

	private static final double INIT_FIELD_PHEROMONE = 1.0;

	private Collection<? extends QuantitySource> sources;

	private Collection<Path> locations;

	public QuantityField(Collection<? extends QuantitySource> sources, Collection<Path> locations) {
		this.sources = sources;
		this.locations = locations;
	}
	
	public void initialize(){
		//initialize field with a homegeneous amount for all edges
		for (Path p : this.locations) {
			Quantity pheromone = p.getPheromone();
			pheromone = pheromone.add(Quantity.newWithDouble(INIT_FIELD_PHEROMONE));

		}
	}

	/**
	 * Implementing P -> P(1-DISSIPATION_FACTOR) + deltaP. deltaP is the
	 * contribution from the sources, which is diffused over the tour
	 * instantaneously, and only on the tour. not physically accurate, but it is
	 * not our aim to simulate diffusion fields.
	 */
	public void updateQuantities() {

		// first apply dissipation to all edges
		for (Path p : this.locations) {
			Quantity pheromone = p.getPheromone();
			pheromone = pheromone.multiply(1.0 - DISSIPATION_FACTOR);

		}

		// now, for all paths in the tours, add the source contributions
		for (QuantitySource source : sources) {

			Tour t = source.getTour();
			Quantity toAdd = source.produce().divide(t.getLength());

			for (Path path : t.getPaths()) {
				Quantity pheromone = path.getPheromone();
				pheromone.add(toAdd);

			}

		}		
	}

}
